/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package post;

import java.io.Serializable;
import java.util.Collections;
import java.util.Vector;

/**
 *
 * @author Stephen
 */
public class ProductCatalog implements Serializable {

    private Vector<Product> catalog;

    public ProductCatalog() {
        catalog = new Vector<Product>();
    }

    /**
     * This method is for adding another item to the catalog.  Mostly used by the ProductReader
     * to populate the catalog.
     * @param upc is the upc of the product
     * @param desc is the description of the product
     * @param price is the price of the product
     */
    public void add(String upc, String desc, float price) {
        catalog.add(new Product(upc, desc, price));
    }

    /**
     * This method is used for getting the price of a product given its UPC
     * @param upc is the UPC of the product
     * @return is the price of the product
     */
    public float getPriceByUpc(String upc) {
        for (int i = 0; i < catalog.size(); i++) {
            if (upc.compareTo(catalog.elementAt(i).getUpc()) == 0) {
                return catalog.elementAt(i).getPrice();
            }
        }
        return -1.F;
    }

    /**
     * This method is used for getting the description of a product given its UPC
     * @param upc is the UPC of the product
     * @return is the description of the product
     */
    public String getDescByUpc(String upc) {
        for (int i = 0; i < catalog.size(); i++) {
            if (upc.compareTo(catalog.elementAt(i).getUpc()) == 0) {
                return catalog.elementAt(i).getDescription();
            }
        }
        return null;
    }

    /**
     * This method is used for getting an array of all the UPCs in the catalog
     * @return is the String array of all UPCs
     */
    public String[] getAllUPC() {
        String[] UPCs = new String[catalog.size()];
        for (int i = 0; i < catalog.size(); i++) {
            UPCs[i] = catalog.elementAt(i).getUpc();
        }
        return UPCs;
    }
    
    /**
     * This method is for converting the product catalog to an array of products.  Used for Transaction because
     * it takes a Product array as a parameter
     */
    public Product[] toArray() {
        int size = catalog.size();
        Product[] productArray = new Product[size];
        for (int i = 0; i < size; i++) {
            productArray[i] = catalog.elementAt(i);
        }
        return productArray;
    }
    
    /**
     * This method is for sorting the catalog by UPC
     */
    public void sortByUpc() {
        Collections.sort(catalog);
    }
}
